//
//  MPSearchViewController.m
//  SearchPeople
//
//  Created by YoloMao on 16/5/10.
//
//

#import "MPSearchViewController.h"
#import "MPImport.h"
#import "YMTypeEffectLabel.h"
#import "MPSearchResultViewController.h"
#import <SVProgressHUD.h>

@interface MPSearchViewController ()
<
UIImagePickerControllerDelegate,
UINavigationControllerDelegate
>

@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) YMTypeEffectLabel *label;

@property (weak, nonatomic) IBOutlet UIButton *faceSearchButton;
@property (weak, nonatomic) IBOutlet UIButton *nameSearchButton;
@property (weak, nonatomic) IBOutlet UIButton *SearchByTextButton;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UIImageView *lineImageView;
@property (weak, nonatomic) IBOutlet UIButton *backButton;

@end

@implementation MPSearchViewController

#pragma mark - UIViewController Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideNameSearch];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

#pragma mark - Action

- (IBAction)faceSearchButtonClick:(id)sender
{
    [UIActionSheet showInView:self.view withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"拍照", @"从相册中选取"] tapBlock:^(UIActionSheet * _Nonnull actionSheet, NSInteger buttonIndex)
    {
        if (buttonIndex == 1)
        {
            [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }else if (buttonIndex == 0)
        {
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
                [self presentViewController:self.imagePicker animated:YES completion:nil];
            }else
            {
                [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                [self presentViewController:self.imagePicker animated:YES completion:nil];
            }
        }
    }];
}

- (IBAction)nameSearchButtonClick:(id)sender
{
    self.faceSearchButton.hidden = YES;
    self.nameSearchButton.hidden = YES;
    [self showLabelWithTitle:@"请输入姓名"];
}

- (IBAction)searchByTextButtonClick:(id)sender
{
    if (!(self.nameTextField.text.length > 0)) {
        [SVProgressHUD showErrorWithStatus:@"请输入名字"];
        return;
    }
    MPSearchResultViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MPSearchResultViewController"];
    targetVC.type = 1;
    targetVC.name = self.nameTextField.text;
    targetVC.title = @"搜索结果";
    [self.navigationController pushViewController:targetVC animated:YES];
}

- (IBAction)backButtonClick:(id)sender
{
    [self hideNameSearch];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
    
    NSData *imgData= UIImageJPEGRepresentation(image,0.1);
    [self pushTargetVCWithData:imgData];//跳转到有搜索结果的界面
    [picker dismissViewControllerAnimated:YES completion:^{
        if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
        }
    }];
}

//保存照片成功后的回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo
{
    NSLog(@"保存图片%@",error != NULL ? @"失败" : @"成功");
}

#pragma mark - Private method

- (void)pushTargetVCWithData:(NSData *)data{
    
    MPSearchResultViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MPSearchResultViewController"];
    targetVC.type = 0;
    targetVC.imageData = data;
    targetVC.title = @"搜索结果";
    [self.navigationController pushViewController:targetVC animated:YES];
}

- (void)showLabelWithTitle:(NSString *)title
{
    self.label = [[YMTypeEffectLabel alloc]init];
    
    self.label.text = title;
    self.label.textColor = CustomTintColor;
    self.label.typewriteTimeInterval = 0.1;//default is 0.3
    [self.view addSubview:self.label];
    
    __weak typeof(self) weakSelf = self;
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.centerY.equalTo(weakSelf.view.mas_centerY).with.offset(-80);
    }];

    self.label.typewriteEffectBlock = ^{
        [weakSelf showNameSearch];
    };
    
    [self.label startType];
}

- (void)showNameSearch
{
    self.nameTextField.hidden = NO;
    self.SearchByTextButton.hidden = NO;
    self.lineImageView.hidden = NO;
    self.backButton.hidden = NO;
}

- (void)hideNameSearch
{
    self.nameTextField.hidden = YES;
    self.SearchByTextButton.hidden = YES;
    self.lineImageView.hidden = YES;
    self.backButton.hidden = YES;
    self.faceSearchButton.hidden = NO;
    self.nameSearchButton.hidden = NO;
    if (self.label) self.label.hidden = YES;
    self.label = nil;
}

#pragma mark - Getter && Setter

- (UIImagePickerController *)imagePicker
{
    if (!_imagePicker)
    {
        _imagePicker = [[UIImagePickerController alloc] init];
        
        _imagePicker.allowsEditing = YES;
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

@end